/*******************************************************************************
* * * Copyright (c) 2013 ASEG Inc.  All Rights Reserved
*  *  *
*  *  * This software is the confidential and proprietary information of
*  *  * ASEG Inc. ("Confidential Information").  You shall not
*  *  * disclose such Confidential Information and shall use it only in
*  *  * accordance with the terms of the license agreement you entered into
*  *  * with ASEG Inc.
*  *  *
*  *  * ASEG Inc. MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
*  *  * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
*  *  * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
*  *  * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. ASEG Inc. SHALL NOT
*  *  * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
*  *  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*  * 
 *  *  * @version     1.0.0
*  *  * @author      Matt Nolan
******************************************************************************/

package org.lionsteins.eddie.server;


import java.util.ArrayList;
import java.sql.Date;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.lionsteins.eddie.domain.Alert;


public class HibernateServiceImpl  {

	
		
		
		 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

		
		public ArrayList getAlerts(Date sDate, Date eDate){
			 return getAlertRecords(sDate, eDate);
		}
		
		public static ArrayList getAlertRecords (Date start, Date end){
	    	Session session = SessionFactoryUtil.getSessionFactory().openSession();
    		
	    	session.beginTransaction();
	    	
	    	Query query = session.createQuery("from Alert where date >= :start and date <= :end");
	    	query.setParameter("start", start);
	    	query.setParameter("end", end);
	    			
	    	ArrayList temp = (ArrayList) query.list();
	    	for (int i=0; i< temp.size(); i++) {
	    		Alert alert = (Alert)temp.get(i);
	    		System.out.println("lat lon: "+ alert.getLat() + "  " + alert.getLon());
	    	}
	    	
		 return temp;
			
		}

		public static boolean addAlert(Alert alert) {
	    	Session session = SessionFactoryUtil.getSessionFactory().openSession();
    		boolean ok = false;
	    	try {
	    		Transaction tx = session.beginTransaction();
	    		Integer id = (Integer)session.save(alert);
	    		tx.commit();
	    		ok = true;
	    	} catch (Exception e) {
	    		
	    	} finally {
	    		if (session != null){
	    			session.close();
	    		}
	    	}
	    	return ok;
	    	
		}
		 
	}
